package DAOs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import Conexao.ConnectionManager;

import modelo.Musica;
import modelo.Musico;

class MusicoDAOImp implements MusicoDAO{

	private final String INSERT = "insert into Musico (nome,ehSolo) values (?,?)";
	private final String UPDATE = "update Musico set nome = ?, codMusico = ?, ehSolo = ? where codMusico = ?";
	private final String DELETE = "delete from Musico where codMusico = ?";
	private final String ALL = "select codMusico, nomeMusico, ehSolo from Musico";
	private final String LIKE = "select codMusico,nomeMusico, ehSolo where codMusico like ?";

	private ConnectionManager connectionManager;
	private static Connection connection;
	
	@Override
	
	public Musico salvar(Musico e) {
		try {
			PreparedStatement ps= null;
			
			ps= connection.prepareStatement(INSERT);
			
			ps.setObject(1,e.getCodMusico());
			ps.setObject(2,e.getCodCd());
			
			ps.executeUpdate();
		} catch (SQLException ex){
			Logger.getLogger(MusicaDAOImp.class.getName()).log(Level.SEVERE, null,ex); 
		}finally{
			this.closeConnection();
		}
		return e;
	}

	@Override
	public void alterar(Musico e) {
		PreparedStatement ps = null;
		try {
			ps = connection.prepareStatement(UPDATE);
			
			ps.setObject(1,e.getCodMusico());
			ps.setObject(2,e.getCodCd());
			
			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(CdDAOImp.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			this.closeConnection();
		}
		
	}

	@Override
	public void deletar(Musico e) {
		try{
			PreparedStatement ps=null;
			ps = connection.prepareStatement(DELETE);
		}catch (SQLException ex){
			Logger.getLogger(MusicoDAOImp.class.getName()).log(Level.SEVERE, null, ex);
		}finally{
			this.closeConnection();
		}
		
		
	}

	@Override
	public List<Musico> findAll() {
List<Musico> musicos = new ArrayList<Musico>();
		
		try{
			PreparedStatement ps=null;
			ResultSet rs=null;
			ps= connection.prepareStatement(ALL);
			
			rs= ps.executeQuery();
			
			while (rs.next()){
				Musico musico = new Musico();
				musico.setNome(rs.getString(1));
				musico.setEhSolo(rs.getBoolean(2));
				musicos.add(musico);
				
			}
		}catch (SQLException ex){
			Logger.getLogger(MusicaDAOImp.class.getName()).log(Level.SEVERE, null, ex);

		}finally{
			this.closeConnection();
		}
		return musicos;
	}

	@Override
	public boolean isConnectionClose() {
		try {
			return connection.isClosed();
		} catch (SQLException ex) {
			return false;
		}
	}

	@Override
	public void openConnection() {
		if ( connectionManager == null ) {
			connectionManager = ConnectionManager.getInstance();
		}
		
		if ( connection == null) {
			connection = connectionManager.getConnection();
		}
		
		if ( isConnectionClose() ) {
			connection = connectionManager.getConnection();
		}	
	}

	@Override
	public void closeConnection() {
		if ( connection != null && connectionManager != null) {
			if ( ! this.isConnectionClose() ) {
				connectionManager.closeConnection(connection);
			}
		}	
	}

	@Override
	public List<Musico> buscaMusicosPorNome(Musico musico) {
		List<Musico> musicos = new ArrayList<Musico>();
		PreparedStatement ps = null;
		ResultSet rs= null;
		 try{
			 ps=connection.prepareStatement(LIKE);
			 ps.setObject(1,musico.getNome());
			 
			 rs=ps.executeQuery();
			 
			 while (rs.next()){
				 musicos.add(new Musico(
						 rs.getInt("codMusico"),
						 rs.getInt("codCd "),
						 rs.getString("nome"),
						 rs.getBoolean("ehSolo")));
			 }
		 }catch(SQLException ex){
				Logger.getLogger(MusicaDAOImp.class.getName()).log(Level.SEVERE, null, ex);
		 }finally{
			 this.closeConnection();
		 }
		return musicos;
	}

}
